<?php
 require_once("../include/httpclient.php");
 require_once("../include/db_mysql.php");
 require_once("../include/sortarray.php");
/*
serviceurl:104
查询返回一个城市的party信息数组,$arg 为Landmarkid和查询字符串
*/
function GetPartyForSearch($arg)
{
	$return = array(); 
    $sortReturn = array();
	$input =stripslashes($arg['input']);
	$landmarkId= $arg['LM'];

    $data=array('LM'=>urlencode(stripslashes($landmarkId)));

    $facilityIdArrayJson = call_serviceurl(106,$data);
    $facilityIdArray = (array)json_decode($facilityIdArrayJson);

	$link = new SqlClass();
	//连接数据库
	$link->connect();
	//执行sql语句
	$partyIdArray=array();	
    $sqlCommand1 = "select partyId,partyName from party where locate('$input',partyName)>0";	
	$rs1 = $link->exec_sql($sqlCommand1);	
    while($row1=mysql_fetch_array($rs1))
	{			
      $arr = GetPartyInfoById($row1,$facilityIdArray,$landmarkId,$link);	
	  if(count($arr)>0)
		{
			array_push($return,$arr);	
		}
	}	
	
		//根据message查找	
      $sqlCommand2 = "select pageId from partypageinfo where locate('$input',message)>0";	
	  $rs2 = $link->exec_sql($sqlCommand2);
      while($row2=mysql_fetch_array($rs2))
	 {
		$pageId = $row2['pageId'];
		$sqlCommand3 ="select partyId,partyName from party where homepageid = $pageId";
		$rs3 = $link->exec_sql($sqlCommand3);      
		while($row3=mysql_fetch_array($rs3))
		{
			if(!IsPartyExist($row3['partyName'],$return))
			{
				$arr = GetPartyInfoById($row3,$facilityIdArray,$landmarkId,$link);
				if(count($arr)>0)
				{
					array_push($return,$arr);
				}
			}
		}
	 }	   	
	$sortReturn = SortArray($return,'distance','asc');
	$link->close();
    return json_encode($sortReturn);
}

/*根据PartyId 返回存储party信息的一维数组 partyName pageUrl facility message
partyId对应的facilityId要再facilityIdArray数组中
*/
function GetPartyInfoById($partyInfo,$facilityIdArray,$landmarkId,$conn)
{
  $flag = false;
  $arr = array();  
  $partyId = $partyInfo['partyId'];
  $sqlCommand1="select facilityId from partyfacility where partyId = $partyId";
  $rs1=$conn->exec_sql($sqlCommand1);
  while($row1 = mysql_fetch_array($rs1))
	{
	  $facilityId = $row1['facilityId'];	  	
	  if(in_array($facilityId,$facilityIdArray))
		{
		$flag=true;
		$arr['partyName']=$partyInfo['partyName'];
		$temp = array("facilityId" => $facilityId);
		$facility = call_serviceurl(105,$temp);	  
		$arr['facility'] = json_decode($facility);
		}
	  break;
	}
if($flag)
  {
	$sqlCommand2 = "select pageUrl,message from partypageinfo where pageid = (select homePageId from party where partyId = $partyId)";		
	$rs2 = $conn->exec_sql($sqlCommand2);      
	while($row2=mysql_fetch_array($rs2))
	{
		$arr['pageUrl']= $row2['pageUrl'];
		$arr['message'] =$row2['message'];
		break;
    }
 
 $sqlCommand3="select distance from partylandmarkdistance where partyId = $partyId and landmarkId=$landmarkId";
 $rs3 = $conn->exec_sql($sqlCommand3);   
 if($row3=mysql_fetch_array($rs3))
	{	
	$arr['distance'] = $row3['distance'];
	}					 	
	else
	{
     $arr['distance'] = -1;
	}
   return $arr;
  }
}

//判断partyName 是否在数组中已经存在
function IsPartyExist($partyName,$partyArr)
{
  for($i=0;$i<count($partyArr);$i++)
 {
   $temp = (array)$partyArr[$i];
   if(in_array($partyName,$temp))
	 {
	   return true;
	 }
 }
 return false;
}


?>